package com.tian.controller;

import com.github.pagehelper.PageInfo;
import com.tian.entity.User;
import com.tian.service.UserService;
import com.tian.service.impl.UserServiceImpl;
import com.tian.util.OssUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: Tian
 * @version: 1.0
 * @since: 1.0
 * @date: 2024/3/6/16:20
 */
@Controller
public class UserController {
    @Resource
    private UserService userServiceImpl;

    @RequestMapping(value = "/gets")
    public String selectAll(@RequestParam(defaultValue = "1") int current,@RequestParam(defaultValue = "2") int pageSize,Model model){
        PageInfo<User> pageInfo= userServiceImpl.selectAll(current,pageSize);
        System.out.println(pageInfo);
        model.addAttribute("pageInfo",pageInfo);
        return "list.jsp";
    }
    
    @RequestMapping(value = "/addUser")
    //String name, String password, String nick, String sex)
    public String addUser(MultipartFile myfile,User user){
        String path = OssUtils.uploadOss(myfile);
        user.setHead(path);
        int count= userServiceImpl.insertUser(user);
        if(count==1){
            return "redirect:gets";
        }else {
            return "addUser.jsp";
        }
    }


    @RequestMapping(value = "/get")
    public String selectById(Long id,Model model){
        User user = userServiceImpl.selectById(id);
        model.addAttribute("u",user);
        return "modify.jsp";
    }

    @RequestMapping(value = "/update")
    public  String update(MultipartFile myfile, User user){
        if(myfile!=null){
            String path = OssUtils.uploadOss(myfile);
            user.setHead(path);
        }
        int count = userServiceImpl.update(user);
        if(count==1){
            return "redirect:gets";
        }else {
            return "modify.jsp?id="+user.getId();
        }

    }

    @RequestMapping(value = "/delete")
    public  String deleteById(Integer id){
        int count= userServiceImpl.deleteById(id.longValue());
        if(count==1){
            return "redirect:gets";
        }else {
            return "list.jsp";
        }

    }


}
